<script>
$(document).ready(function(){
    var member = <?php echo $this->member?>;

	function checkMember()
	{
		var currentMember = parseInt($("#currentMember").html());

		if (currentMember > member) {
			$("#saveStudentBtn").attr('disabled','disabled').addClass('ui-state-disabled');
			jAlert("<?php echo $this->translate('selected over')?>");
		}
		else
			 $("#saveStudentBtn").attr('disabled','').removeClass('ui-state-disabled');
	}

	$("#appendForm").ajaxForm({
	    dataType:  'json',
	    success:  function(data){
    	    if (data.success) {
    	        $("#select-subject").trigger('change');
    	        $("#sel_class").trigger('change');
    	    }
	    }
    });


    $("form#appendForm input").filter(":checkbox").click(function(){
        var i = parseInt($("#currentMember").text());
        var p = $(this).parents('tr');
        if ($(this).attr('checked')=='') {
            i--;
            p.removeClass('selected');
        }
        else {
            i++;
            p.addClass('selected');
        }
    	$("#currentMember").html(i);
         checkMember();
      });


    $("#saveStudentBtn").click(function(){
        $("#appendForm").submit();
       });

    $("#closeStudentBtn").click(function(){
    	$('#dialog').dialog('close');
      });

    $("form#appendForm input").filter(":checkbox").checkbox();



    checkMember();

});
</script>
<table>
<tr>
<td>
<a  id="saveStudentBtn"  href="#" class="fg-button ui-state-default  fg-button-icon-left ui-corner-all">
<span class="ui-icon ui-icon-disk"></span>
<?php echo $this->translate('save')?>
</a>
</td>
<td>
<a  id="closeStudentBtn"  href="#" class="fg-button ui-state-default  fg-button-icon-left ui-corner-all">
<span class="ui-icon ui-icon-close"></span>
<?php echo $this->translate('close')?>
</a>
</td>
<td>
<span id="currentMember"><?php echo $this->currentMember?></span> / <span id="member"><?php echo $this->member ?></span>
</td>
</tr>
</table>

<div  style="overflow: scroll; height:350px">
<form action="<?php echo $this->route('append-student')?>" method="post" id="appendForm">
<table id="student-select-table" class="hor-minimalist-b" style="margin:1px;width:300px">
<thead>
<tr>
<th></th>
<th><?php echo $this->translate('student_number')?></th>
<th><?php echo $this->translate('student_id')?></th>
<th><?php echo $this->translate('student_name')?></th>
</tr>
</thead>
<tbody>
<?php foreach($this->data as $row):?>
<tr <?php if (!isset($row['selSn'])  or $row['selSn']===null):?>class="select-student"<?php endif?>>
<td>
<?php if (!isset($row['selSn']) or $row['selSn']===null):?>
<input type="checkbox"  name="studentSn[]"  value="<?php echo $row['sn']?>" />
 <?php endif;?>
</td>
<td class="<?php echo ($row['sex']=='M')?'boy':'girl'?>"><?php echo $row['number']?></td>
<td><?php echo $row['student_id']?></td>
<td><?php echo $row['name']?></td>
</tr>
<?php endforeach?>
</tbody>
</table>
<input type="hidden"  name="group_sn" id="group_sn"  value="<?php echo $this->group_sn?>"/>
</form>
</div>